/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.consultorio.dao;

import br.com.consultorio.bean.Consulta;
import br.com.consultorio.bean.Exame;
import br.com.consultorio.bean.MaterialUso;
import br.com.consultorio.bean.Pessoa;
import br.com.consultorio.util.FabricaConexao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Thiago
 */
public class ExameDaoImp implements ExameDao {
    private Connection conn;
    private PreparedStatement ps;

    @Override
    public Object salvar(Object obj) throws Exception {
        Exame exame = (Exame) obj;
        PreparedStatement ps = null;
        Connection conn = null;
        ResultSet rs = null;
        ExameDao exaDao = new ExameDaoImp();
        try {
            String SQL = "INSERT INTO exame(data, fornecedor, consulta_id)"
                    + "VALUES(?, ?, ?)";
            conn = FabricaConexao.abreConexao();
            ps = conn.prepareStatement(SQL, Statement.RETURN_GENERATED_KEYS);
            ps.setString(1, exame.getData());
            ps.setString(2, exame.getFornecedor());
            ps.setInt(3, exame.getConsulta().getId());
            ps.executeUpdate();
            //rs = ps.getGeneratedKeys();
           // rs.next();// para entrar na dentro do resultados
           // exame.setId(rs.getInt(1));
            //chama as daos de endereco e usuario para salvar em sua tabelas 
            //passando o id de pessoa
            //exaDao.salvar(exame);
            
        } catch (SQLException se) {
            throw new Exception("Erro ao tentar inserir dados" + se.getMessage());
        } finally {
            FabricaConexao.fechaConexao(conn, ps);
        }
        return exame;
    }

    @Override
    public void alterar(Object obj) throws Exception {
        Exame exame = (Exame) obj;
        
        try {            
            String SQL = "UPDATE exame SET data=?, fornecedor=?, consulta_id=? WHERE id=?";
            conn = FabricaConexao.abreConexao();
            ps = conn.prepareStatement(SQL);
            
            ps.setString(1, exame.getData());
            ps.setString(2, exame.getFornecedor());
            ps.setInt(3, exame.getConsulta().getId());
            
            ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception("Exame - Erro ao ALTERAR "
                    + e.getMessage());
        } finally {
            FabricaConexao.fechaConexao(conn, ps);
        }
    }

    @Override
    public void excluir(int id) throws Exception {
         Connection conn = null;
        PreparedStatement ps = null;
        
        try {
            String SQL = "DELETE FROM exame WHERE id = ?";
            conn = FabricaConexao.abreConexao();
            ps = conn.prepareStatement(SQL);
            
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception("Exame - Erro ao EXCLUIR "
                    + e.getMessage());
        } finally {
            FabricaConexao.fechaConexao(conn, ps);
        }
    }

    @Override
    public Object procurar(Integer id) throws Exception {
        PreparedStatement ps = null;
        Connection conn = null;
        ResultSet rs = null;
        Exame exame = null;
        Consulta consulta = new Consulta();
        try {
            String SQL = "SELECT * FROM exame exa JOIN "
                    + "consulta con ON "
                    + "exa.consulta_id = con.id WHERE exa.id=?";
            conn = FabricaConexao.abreConexao();
            ps = conn.prepareStatement(SQL);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                exame = new Exame();
                exame.setId(id);
                exame.setData(rs.getString("exa.data"));
                exame.setFornecedor(rs.getString("exa.fornecedor"));
                consulta.setId(rs.getInt("con.id"));
            }
        } catch (SQLException se) {
            throw new Exception("Erro ao tentar atualizar dados"
                    + se.getMessage());
        } finally {
            FabricaConexao.fechaConexao(conn, ps, rs);
        }
        return exame;
    }

    @Override
    public List<Object> listar() throws Exception {
        PreparedStatement ps = null;
        Connection conn = null;
        ResultSet rs;
        Exame exame;
        List exames = new ArrayList();
        Consulta consulta = new Consulta();
        try {
            String SQL = "SELECT * FROM exame";
            conn = FabricaConexao.abreConexao();
            ps = conn.prepareStatement(SQL);
            rs = ps.executeQuery();
            while (rs.next()) {
                exame = new Exame();
                exame.setId(rs.getInt("id"));
                exame.setData(rs.getString("data"));
                exame.setFornecedor(rs.getString("fornecedor"));
                consulta.setId(rs.getInt("id"));
                exames.add(exame);
            }
        } catch (SQLException se) {
            throw new Exception("Erro ao tentar atualizar dados"
                    + se.getMessage());
        } finally {
            FabricaConexao.fechaConexao(conn, ps);
        }
        return exames;
    }
    
}
